# -*- coding: UTF-8 -*- #
"""
-------真正的勇士， 敢于直面惨淡的Warning、 敢于正视淋漓的Error-----
@author : 贾鹏博
@file : 6、接口请求构造.py
@time : 2022/5/14 20:06
"""

# 请求目标构造

import requests


r = requests.get('https://api.github.com/events')

"""
请求参数构造
    请求参数构造(get query) ： path  query
    请求体(post body) :
        form
        结构化请求 ：json , xml , json rpc
        binary

"""


class TestDemo:

    # =================================  get 的 quary 参数 : params 参数 ===============================================
    # https://httpbin.testing-studio.com/get?level=1&name=seveniruby
    def test_query(self):
        payload = {
            "level": 1,
            "name": "seveniruby"
        }
        r = requests.get('https://httpbin.testing-studio.com/get', params=payload)
        """
        "args": {
            "level": "1", 
            "name": "seveniruby"
        }
        """
        print(r.text)
        assert r.status_code == 200

    # ======================================  post 的 form 请求  =======================================================
    def test_post_form(self):
        payload = {
            "key1": "value1",
            "key2": "value2"
        }
        r = requests.post("https://httpbin.org/post", data=payload)
        """
        "form": {
            "key1": "value1", 
            "key2": "value2"
            }
        """
        print(r.text)
        assert r.status_code == 200

    # =====================================  文件上传  =========================================================
    def test_file(self):
        files = {'file': open('./search_res.png', 'rb')}
        r = requests.post(url='https://www.baidu.com', files=files)
        assert r.status_code == 200

    # ========================================   header 构造  ========================================================
    def test_header(self):
        headers = {'user-agent': 'my-app/0.0.1'}
        r = requests.get(url='https://httpbin.testing-studio.com/get', headers=headers)
        print(r.text)
        assert r.status_code == 200
        assert r.json()['headers']['User-Agent'] == 'my-app/0.0.1'

    # ================================================ cookie ==========================================================
    def test_cokkies(self):
        cookies = dict(cookies_are='working')
        res = requests.get(url='https://www.baidu.com', cookies=cookies)
        print(res.text)
